<?php if (!defined('SERVER_ROOT')) exit('No direct script access allowed');

class giaotrinh_Controller extends Controller {

    function __construct()
    {
        parent::__construct('cvpnckh', 'giaotrinh');
        $this->check_login();
        $this->view->template->menu_html =$this->CREATE_MENU();
        $this->view->template->nhacviec_html =$this->CREATE_NHAC_VIEC(); 
        $this->CHECK_QUYEN('cvpnckh', 'giaotrinh');
    }

    function main(){
        $this->dsp_all_giaotrinh();
    }
    private function _save_filter(){
        $v_TYPE_filter = $this->get_post_var('hdn_TYPE_filter');
        $v_MA_DON_VI_filter = $this->get_post_var('cbx_MA_DON_VI_filter');
        $v_MA_TO_CHUYEN_MON_filter = $this->get_post_var('cbx_MA_TO_CHUYEN_MON_filter');
        $v_MA_CAN_BO_filter = $this->get_post_var('cbx_MA_CAN_BO_filter');
        if($v_TYPE_filter == ''){
           $v_TYPE_filter = 'MA_DON_VI';
           $v_MA_TO_CHUYEN_MON_filter = 0;
        }
        if($v_MA_TO_CHUYEN_MON_filter == ''){
           $v_MA_TO_CHUYEN_MON_filter = 0;
        }
        if($v_MA_CAN_BO_filter == ''){
           $v_MA_CAN_BO_filter = 0;
        }
        return array(
            'hdn_TYPE_filter'   => $v_TYPE_filter,
            'cbx_MA_DON_VI_filter'=> $v_MA_DON_VI_filter,
            'cbx_MA_TO_CHUYEN_MON_filter'=> $v_MA_TO_CHUYEN_MON_filter,
            'cbx_MA_CAN_BO_filter'=> $v_MA_CAN_BO_filter,
        );
    }

    function dsp_all_giaotrinh(){
        $arr_filter = $this-> _save_filter();
        if ($arr_filter['hdn_TYPE_filter'] == 'MA_DON_VI'){
            $P_MA_DON_VI = $arr_filter['cbx_MA_DON_VI_filter'];
            $VIEW_DATA['arr_filter']       = $arr_filter;
            $VIEW_DATA['arr_all_donvi_filter']       = $this->model->SELECT_DONVI_chon();
            $VIEW_DATA['arr_all_tochuyenmon_filter'] = $this->model->SELECT_TOCHUYENMON_by_mdv($P_MA_DON_VI);
            $VIEW_DATA['arr_all_canbo_filter'] = $this->model->SELECT_CAN_BO_OF_TO_CHUYEN_MON(0);
            $VIEW_DATA['arr_all_giaotrinh']= $this->model->SELECT_GIAO_TRINH_BY_MA_DON_VI($P_MA_DON_VI);

        }else if($arr_filter['hdn_TYPE_filter'] == 'MA_TO_CHUYEN_MON'){
            $P_MA_DON_VI = $arr_filter['cbx_MA_DON_VI_filter'];
            $P_MA_TO_CHUYEN_MON = $arr_filter['cbx_MA_TO_CHUYEN_MON_filter'];
            $VIEW_DATA['arr_filter']       = $arr_filter;
            $VIEW_DATA['arr_all_donvi_filter']       = $this->model->SELECT_DONVI_chon();
            $VIEW_DATA['arr_all_tochuyenmon_filter'] = $this->model->SELECT_TOCHUYENMON_by_mdv($P_MA_DON_VI);
            $VIEW_DATA['arr_all_canbo_filter'] = $this->model->SELECT_CAN_BO_OF_TO_CHUYEN_MON($P_MA_TO_CHUYEN_MON);
            $VIEW_DATA['arr_all_giaotrinh']= $this->model->SELECT_GIAO_TRINH_BY_MA_DON_VI($P_MA_DON_VI);

        }else{
            $P_MA_DON_VI = $arr_filter['cbx_MA_DON_VI_filter'];
            $P_MA_TO_CHUYEN_MON = $arr_filter['cbx_MA_TO_CHUYEN_MON_filter'];
            $P_MA_CAN_BO = $arr_filter['cbx_MA_CAN_BO_filter'];
            $VIEW_DATA['arr_filter']       = $arr_filter;
            $VIEW_DATA['arr_all_donvi_filter']       = $this->model->SELECT_DONVI_chon();
            $VIEW_DATA['arr_all_tochuyenmon_filter'] = $this->model->SELECT_TOCHUYENMON_by_mdv($P_MA_DON_VI);
            $VIEW_DATA['arr_all_canbo_filter'] = $this->model->SELECT_CAN_BO_OF_TO_CHUYEN_MON($P_MA_TO_CHUYEN_MON);
            if ($P_MA_CAN_BO!=0){
               $VIEW_DATA['arr_all_giaotrinh']= $this->model->SELECT_GIAO_TRINH_BY_MA_CAN_BO($P_MA_CAN_BO);
            }else if ($P_MA_TO_CHUYEN_MON!=0){
               $VIEW_DATA['arr_all_giaotrinh']= $this->model->SELECT_GIAO_TRINH_BY_MA_TO($P_MA_TO_CHUYEN_MON);
            }else{
               $VIEW_DATA['arr_all_giaotrinh']= $this->model->SELECT_GIAO_TRINH_BY_MA_DON_VI($P_MA_DON_VI);
            }
        }
        $this->view->render('dsp_all_giaotrinh', $VIEW_DATA);
    }
    public function dsp_single_giaotrinh($TEN_GIAO_TRINH){
        $TEN_GIAO_TRINH = replace_bad_char($TEN_GIAO_TRINH);
        $P_MA_CAN_BO = replace_bad_char($_REQUEST['macanbo']);
        $P_TEN_CAN_BO = replace_bad_char($_REQUEST['tencanbo']);
        $VIEW_DATA['var_macanbo'] = $P_MA_CAN_BO;
        $VIEW_DATA['var_tencanbo'] = $P_TEN_CAN_BO;
        $VIEW_DATA['arr_all_loaigiaotrinh'] = $this->model->SELECT_LOAIGIAOTRINH_nothing();
        $VIEW_DATA['arr_single_giaotrinh'] = $this->model->qry_single_giaotrinh($TEN_GIAO_TRINH,$P_MA_CAN_BO);
        $this->view->render('dsp_single_giaotrinh', $VIEW_DATA);
    }
    public function update_giaotrinh()
    {
        $this->model->goback_url = $this->view->get_controller_url() . 'dsp_all_giaotrinh';
        $this->model->update_giaotrinh();
    }
    public function delete_giaotrinh(){
        $this->model->goback_url = $this->view->get_controller_url() . 'dsp_all_giaotrinh';
        $this->model->delete_giaotrinh();
    }
    public function duyet_giaotrinh(){
        $this->model->goback_url = $this->view->get_controller_url() . 'dsp_all_giaotrinh';
        $this->model->duyet_giaotrinh();
    }
}